package interficie;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ListModel;
import javax.swing.JInternalFrame;

public class PantallaEspera extends JPanel {

	private DefaultListModel lm = new DefaultListModel();
	private JList list = new JList(lm);
	private JScrollPane sclist = new JScrollPane();
	private CtrlInterficie controladorInterficie;
	private DefaultListModel lmOpt = new DefaultListModel();
	private JButton b_guardar = new JButton("Guardar Listado Ordenado");
	private JLabel le_costeFinal = new JLabel("120 minutos");
	private JList listOpt;
	private JPanel panelGuardado;
	private JLabel le_costeInicial = new JLabel("120 minutos");
	private JButton b_recalcular = new JButton("Recalcular");
	
	
	public PantallaEspera(CtrlInterficie ctrlInterficie, ArrayList<String> listado) {
		controladorInterficie = ctrlInterficie;
		
		setBackground(new Color(0, 153, 255));
		setLayout(null);
		setBounds(100, 100, 714, 437);
		
		JLabel lblNewLabel = new JLabel("Calculando Orden \u00D3ptimo");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblNewLabel.setBounds(0, 42, 668, 31);
		add(lblNewLabel);
		
		JLabel lblListaDeTareas = new JLabel("Lista de Tareas Inicial");
		lblListaDeTareas.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblListaDeTareas.setHorizontalAlignment(SwingConstants.CENTER);
		lblListaDeTareas.setBounds(55, 112, 186, 14);
		add(lblListaDeTareas);

		
		sclist.setBounds(55, 137, 186, 134);
		sclist.setBackground(new Color(0, 153, 255));
		add(sclist);
        
        list.setLayoutOrientation(JList.VERTICAL);
        list.setCellRenderer(new DefaultListCellRenderer(){
		       public int getHorizontalAlignment() {
		                return CENTER;
		       }
		});
		list.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
		list.setFont(new Font("Tahoma", Font.BOLD, 14));
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setBackground(new Color(0, 153, 255));
		list.setBounds(273, 144, 145, 134);
		
		sclist.setViewportView(list);
		rellenarListaPrincipal(listado);
		
		JLabel lblCosteFinal = new JLabel("Coste Final:");
		lblCosteFinal.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblCosteFinal.setBounds(465, 289, 109, 14);
		add(lblCosteFinal);
		
		
		le_costeFinal.setFont(new Font("Tahoma", Font.PLAIN, 15));
		le_costeFinal.setBounds(547, 314, 119, 14);
		add(le_costeFinal);
		
		JLabel lblListaDeTareasFinal = new JLabel("Lista de Tareas Final");
		lblListaDeTareasFinal.setHorizontalAlignment(SwingConstants.CENTER);
		lblListaDeTareasFinal.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblListaDeTareasFinal.setBounds(465, 112, 186, 14);
		add(lblListaDeTareasFinal);
		
		
		
		
		JLabel lblCosteInicial = new JLabel("Coste Inicial:");
		lblCosteInicial.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblCosteInicial.setBounds(55, 282, 109, 14);
		add(lblCosteInicial);
		
		
		le_costeInicial.setFont(new Font("Tahoma", Font.PLAIN, 15));
		le_costeInicial.setBounds(137, 307, 119, 14);
		add(le_costeInicial);
		
		
		b_guardar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
					controladorInterficie.mostrarCrearGraph();
			}
		});
		b_guardar.setBounds(282, 352, 186, 23);
		add(b_guardar);
		
		JButton btnVolverAlInicio = new JButton("Volver al Inicio");
		btnVolverAlInicio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controladorInterficie.Volver();
			}
		});
		btnVolverAlInicio.setBounds(282, 383, 186, 23);
		add(btnVolverAlInicio);
		
		
		b_recalcular.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> lista = new ArrayList<String>();
				getListadoOpt(lista);
				le_costeFinal.setText(""+controladorInterficie.recalcularGraph(lista) + " minutos");
				
			}
		});
		b_recalcular.setBounds(547, 352, 89, 23);
		b_recalcular.setVisible(false);
		add(b_recalcular);
		
	}

	public void rellenarListaPrincipal(ArrayList<String> listado) {
		for(String a : listado){
			lm.addElement(a);
		}
	}

	public void modificarListadoOpt(ArrayList<String> resultado) {
		String costeFinal = resultado.get(resultado.size()-2);
		String costeInicial = resultado.get(resultado.size()-1);
		le_costeFinal.setText(costeFinal + " minutos");
		le_costeInicial.setText(costeInicial + " minutos");
		resultado.remove(resultado.size()-1);
		resultado.remove(resultado.size()-1);
		lmOpt.clear();
		for(String a : resultado){
			lmOpt.addElement(a);
		}
		crearListadoFinal(lmOpt);
		
		
		
	}
	private void crearListadoFinal(DefaultListModel lmOpt) {
		JScrollPane sclistfinal = new JScrollPane();
		sclistfinal.setBackground(new Color(0, 153, 255));
		sclistfinal.setBounds(465, 137, 186, 134);
		add(sclistfinal);
		
		listOpt= new JList(lmOpt);
		listOpt.setCellRenderer(new DefaultListCellRenderer(){
	       public int getHorizontalAlignment() {
	                return CENTER;
	       }
		});
		listOpt.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listOpt.setLayoutOrientation(JList.VERTICAL);
		listOpt.setFont(new Font("Tahoma", Font.BOLD, 14));
		listOpt.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
		listOpt.setBackground(new Color(0, 153, 255));
		listOpt.setBounds(0, 0, 184, 132);
		MouseDragger myMouseAdaptor = new MouseDragger(listOpt, lmOpt);
        listOpt.addMouseListener(myMouseAdaptor);
        listOpt.addMouseMotionListener(myMouseAdaptor);
        listOpt.addListSelectionListener(new ListSelectionListener() {			
		      public void valueChanged(ListSelectionEvent e) {
		        if (e.getValueIsAdjusting())
		          return;
		        System.out.println("actual dato " + listOpt.getSelectedValue());
		        b_recalcular.setVisible(true);
		      }

		    });
		sclistfinal.setViewportView(listOpt);
	}

	
	public ArrayList<String> getListadoOpt(ArrayList<String> lista) {
		for(int i = 0; i < lmOpt.size(); ++i){
			lista.add(lmOpt.get(i).toString());
		}
		return lista;
	}
}
